package com.nd.service;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nd.dao.GoodsMapper;
import com.nd.dao.GrouponMapper;
import com.nd.dao.GrouponRulesMapper;
import com.nd.pojo.bean.*;
import com.nd.pojo.bo.GroupAddBo;
import com.nd.pojo.bo.GrouponData;
import com.nd.pojo.vo.GrouponCreateVO;
import com.nd.pojo.vo.GrouponListRecordVO;
import com.nd.pojo.vo.GrouponListRecordVOO;
import com.nd.pojo.vo.GrouponRulesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class GrouponServiceImpl implements GrouponService{
    @Autowired
    GrouponRulesMapper grouponRulesMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GrouponMapper grouponMapper;
    @Override
    public GrouponRulesVo queryGrouponRulesVo(GrouponData grouponData) {
        PageHelper.startPage(grouponData.getPage(),grouponData.getLimit());
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        grouponRulesExample.setOrderByClause(grouponData.getSort()+" "+grouponData.getOrder());
        GrouponRulesExample.Criteria criteria = grouponRulesExample.createCriteria();
        if(grouponData.getGoodsId()!=null){
            criteria.andGoodsIdEqualTo(grouponData.getGoodsId());
        }
        criteria.andDeletedEqualTo(false);
        List<GrouponRules> grouponRulesList = grouponRulesMapper.selectByExample(grouponRulesExample);
        PageInfo<GrouponRules> grouponRulesPageInfo = new PageInfo<>(grouponRulesList);
        long total = grouponRulesPageInfo.getTotal();
        return new GrouponRulesVo(total,grouponRulesList);
    }

    @Override
    public int deleteRules(GrouponRules rules) {
        rules.setDeleted(true);
        Date now = new Date();
        rules.setExpireTime(now);
        rules.setUpdateTime(now);
        int deleteRow = grouponRulesMapper.updateByPrimaryKeySelective(rules);
        if(deleteRow == 0) return 10000;
        return 0;
    }

    @Override
    public GrouponCreateVO addRules(GroupAddBo rules) {
        Goods goods = goodsMapper.selectByPrimaryKey(rules.getGoodsId());
        //if (goods == null) return null;
        Date now = new Date();
        GrouponCreateVO grouponCreateVO = GrouponCreateVO.builder()
                .goodsId(rules.getGoodsId())
                .discount(rules.getDiscount())
                .discountMember(rules.getDiscountMember())
                .expireTime(rules.getExpireTime())
                .goodsName(goods.getName())
                .addTime(now)
                .updateTime(now)
                .picUrl(goods.getPicUrl()).build();
        Integer key = grouponRulesMapper.selectKey();
        grouponRulesMapper.insertGroupon(grouponCreateVO);
        grouponCreateVO.setId(key);
        return grouponCreateVO;
    }

    @Override
    public void grouponUpdate(GrouponRules grouponRules) {
        grouponRules.setUpdateTime(DateTime.now());
        grouponRulesMapper.updateByPrimaryKey(grouponRules);

    }

    //TODO 要去造数据，再去三表查询，💪
    @Override
    public GrouponListRecordVO grouponListRecord(Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page,limit);
        GrouponExample grouponExample = new GrouponExample();
        grouponExample.setOrderByClause(sort + " " + order);
        List<Groupon> groupons = grouponMapper.selectByExample(grouponExample);
        PageInfo<Groupon> grouponPageInfo = new PageInfo<>(groupons);

        ArrayList<GrouponListRecordVOO> grouponListRecordVOOS = new ArrayList<>();
        GrouponListRecordVOO grouponListRecordVOOO = new GrouponListRecordVOO();

        GrouponListRecordVOO.SubGroupon subGroupon = GrouponListRecordVOO.SubGroupon.builder().orderId(1).userId(1).length(1).build();
        GrouponListRecordVOO.GrouponBean grouponBean = GrouponListRecordVOO.GrouponBean.builder().orderId(1).userId(1).build();
        ArrayList<GrouponListRecordVOO.SubGroupon> subGroupons = new ArrayList<>();
        subGroupons.add(subGroupon);
        GrouponListRecordVOO.RulesBean rulesBean = GrouponListRecordVOO.RulesBean.builder().discount(new BigDecimal(5)).discountMember(5).addTime(DateTime.now()).expireTime(DateTime.now()).build();


        grouponListRecordVOOO.setGroupon(grouponBean);
        grouponListRecordVOOO.setRules(rulesBean);
        grouponListRecordVOOO.setSubGroupons(subGroupons);
        grouponListRecordVOOO.setIconUrl("http://project2mall-oss.oss-cn-chengdu.aliyuncs.com/image/jpeg/2021-06-07/9308ce71-83fb-4874-8267-3b5014b43b95-u=4264181808,2128553240&fm=26&gp=0.jpg");

        grouponListRecordVOOS.add(grouponListRecordVOOO);

        long total = grouponPageInfo.getTotal();
        GrouponListRecordVO grouponListRecordVO = new GrouponListRecordVO();
        grouponListRecordVO.setItems(grouponListRecordVOOS);
        grouponListRecordVO.setTotal(total);
//
//        HashMap<String, Object> mm = new HashMap<>();
//        mm.put("orderId",15);
//        mm.put("userId",11);
//        mm.put("member",5);
//        mm.put("discount",10);
//        mm.put("discountMember",10);
//        mm.put("picUrl","http://yanxuan.nosdn.127.net/c8ca0600fa7ba11ca8be6a3173dd38c9.png");
//        mm.put("startTime",DateTime.now());
//        mm.put("endTime",DateTime.now());

        return grouponListRecordVO;
    }
}
